package com.itheima.controller;


import com.itheima.health.resource.entity.Result;
import com.itheima.health.resource.poi.POIUtils;
import com.itheima.health.resource.pub.MessageConstant;
import com.itheima.health.resource.pub.RedisConstant;
import com.itheima.health.resource.upload.UploadUtils;
import com.itheima.health.service.OrderSettingService;
import com.itheima.health.service.SetmealService;
import com.itheima.pojo.Setmeal;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("ordersetting")
public class OrderSettingController {


    @Reference
    private OrderSettingService orderSettingService;


    @RequestMapping("importOrderSettings.do")
    public Result upload(@RequestParam("excelFile") MultipartFile file){
        try {

            //创建list集合，把读取到的数封装到集合中
            List<String[]> list = POIUtils.readExcel(file);
            //调用业务层的方法转换list的格式，将其封装到数据库中
            orderSettingService.bachOrderSetting(list);
            return  new Result(true, MessageConstant.EDIT_ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false, MessageConstant.EDIT_ORDERSETTING_FAIL);
        }

    }


    /**
     *    根据年和月 查询预约数据表    具体月份 对应  最大预约数据 和已经预约数据 +  几号  List<Map>
     * @param
     * @return
     */
    @RequestMapping("findOrderSettingsData.do")
    public Result findOrderSettingsData(@RequestParam("yearAndMonth") String  yearAndMonth){
        try {

            List<Map>  map  = orderSettingService.findOrderSettingsData(yearAndMonth);

            return  new Result(true, MessageConstant.QUERY_ORDERSETTING_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false, MessageConstant.QUERY_ORDERSETTING_FAIL);
        }

    }

    @RequestMapping("updateOrderSettingData.do")
    public Result upload(@RequestParam("dateparam")String dateparam, @RequestParam("num") Integer num){
        try {
            orderSettingService.updateOrderSettingData(dateparam,num);
            return  new Result(true, MessageConstant.EDIT_ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false, MessageConstant.EDIT_ORDERSETTING_FAIL);
        }

    }

}
